package com.wisdytech.linkdcs.account.action;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.account.model.TtEqAccount;
import com.wisdytech.linkdcs.account.service.IAccountRegisterService;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.equipment.service.IEquipmentRegisteredService;
import com.wisdytech.linkdcs.system.model.SysUser;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * 台账登记控制层controller
 * @author zhanghailin
 *
 */
@Controller
@RequestMapping("/equipment/account")
public class AccountRegisterAction {

    @Resource
    private IAccountRegisterService accountRegisterService;
    
    @Autowired
    private IEquipmentRegisteredService iEquipmentRegisteredService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
     *
     * <p>Description: 设备信息</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/index")
    public String equipmentIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "platformAccount/equipInfo";
    }

    /**
     *
     * @param equipCode 设备编码
     * @param equipName 设备名称
     * @param operationId 工序Id
     * @param supplier 供应商
     * @param filter 分页参数
     * @return StandardTableResult
     */
    @RequestMapping(value = "/listEquipInfoByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<Map<String, Object>> listEquipInfoByFilter(String equipCode, String equipName,String operationId,String supplier, Filter filter) {
        PageInfo<Map<String, Object>> page = this.accountRegisterService.listEquipInfoByFilter(equipCode,equipName,operationId,supplier,filter);
        return new StandardTableResult<>(page.getTotal(),page.getList());
    }

    /**
     * 根据设备Code查询设备信息
     * @param equipCode 设备code
     * @return StandardResult
     */
    @RequestMapping(value = "/getEquipInfoByCode/get.mvc")
    @ResponseBody
    public StandardResult getEquipInfoByCode(String equipCode) {
        Map<String, Object> equipInfo = accountRegisterService.getEquipInfoByCode(equipCode);
        return StandardResult.getSuccessResult(equipInfo);
    }

    /**
     * 台账登记
     * @param ttEqAccount 实体类
     * @return 
     * @return StandardResult
     */
    @RequestMapping(value = "/saveOrUpdateEquipInfo/save.mvc")
    @ResponseBody
    public  StandardResult saveOrUpdateEquipInfo(TtEqAccount ttEqAccount) {
        SysUser user = BaseUtil.getCurrentUser();
        this.accountRegisterService.accountRegister(ttEqAccount,user);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询设备类别类型信息列表</p> 
     * @return 设备类别类型信息列表集合
     */
    @RequestMapping(value = "/searchEquipAcByEquipCode/get.mvc")
    @ResponseBody
    public StandardResult searchEquipAcByEquipCode(@ModelAttribute("search") String search,Filter filter){
    	List<TtEqAccount> lists = accountRegisterService.searchEquipAcByEquipCode(search,filter);
        return StandardResult.getSuccessResult(lists);
    }
    
    /***
     * 分页查询辅助类设备
     * @param search
     * @param equipCode 生产类设备id //关联查询工序一致
     * @return
     */
    @RequestMapping(value = "/searchEquipByType/get.mvc")
    @ResponseBody
    public StandardResult searchAidEquips(@ModelAttribute("search") String search,
    		@ModelAttribute("num") int num,
    		@ModelAttribute("equipCode") String equipCode) {
    	
    	Filter filter =new Filter();
    	filter.setNum(num);
    	filter.setSize(10);
    	
    	
    	PageInfo<Equipment>  pageInfos=
    			iEquipmentRegisteredService.searchEquipByType(filter, search, "5",equipCode);
    	
    	 //判断是否是最后一页
        if(filter.getNum()>=pageInfos.getPages()){
            return StandardResult.getFailResult("",pageInfos.getList());
        }else{
            return StandardResult.getSuccessResult(pageInfos.getList());
        }
    }
    
    @RequestMapping("/delAidEquip/del.mvc")
    @ResponseBody
    public StandardResult delAidEquip(String equipCode,String aidEquipCode) {
    	if(StringUtils.isBlank(equipCode)||StringUtils.isBlank(aidEquipCode)) {
    		return StandardResult.getFailResult("设备编码为空");
    	}
    	
    	iEquipmentRegisteredService.delAidEquip(equipCode, aidEquipCode);
    	return StandardResult.getSuccessResult();
    	
    		
    }
    


}
